package com.zbcz.crm

import android.app.DatePickerDialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.viewModels
import com.zbcz.crm.adapter.VisitDataListAdapter
import com.zbcz.crm.bean.FollowPersonVo
import com.zbcz.crm.bean.VisitData
import com.zbcz.crm.databinding.ActivityVisitorListBinding
import com.zbcz.crm.util.IS_TEST_MODE
import com.zbcz.crm.util.formatDate
import com.zbcz.crm.util.getCurrentDateFormatted
import com.zbcz.crm.util.onSingleClick
import com.zbcz.crm.viewmodel.HomePageViewModel
import java.util.Calendar


class VisitOrderListActivity : BaseActivity() {

    private lateinit var mBinding: ActivityVisitorListBinding

    private val mHomePageViewModel: HomePageViewModel by viewModels()

    companion object {
        const val TAG = "VisitOrderListActivity"

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityVisitorListBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        init()
    }

    private fun init() {
        //adjustLayoutForStatusBar(this, mBinding.homeWrapper)
        mBinding.tvSearch.onSingleClick {
            mHomePageViewModel.getVisitList(mBinding.edVisitorInput.text.toString())
        }
        mHomePageViewModel.mVisitDataLiveData.observe(this) {
            Log.i(TAG, it.toString())
            mBinding.mList.adapter = VisitDataListAdapter(it)
            //mBinding.mList.adapter = VisitDataListAdapter(generateFakeVisitDataList(4))
            mBinding.emptyGroup.visibility = if (it.isNullOrEmpty()) View.VISIBLE else View.GONE
        }
        mBinding.tvDate.onSingleClick {
            showDatePickerDialog(this@VisitOrderListActivity)
        }
        mBinding.tvDate.text = getCurrentDateFormatted()
        mBinding.ivBack.onSingleClick {
            finish()
        }
    }

    fun createTestData(): List<VisitData> {
        val testData = mutableListOf<VisitData>()

        testData.add(
            VisitData(
                carryingElectronic = "Yes",
                visitArea = "Building A",
                visitDate = "2024-04-01",
                visitOrderStatus = "1",
                visitPurpose = "Meeting",
                visitingPersonMobileNo = "1234567890",
                visitingPersonName = "John Doe",
                visitorEmployer = "ABC Company",
                visitorIdNo = "1234567890123456",
                visitorMobileNo = "9876543210",
                visitorName = "Alice Smith",
                followPersonVos = null
            )
        )

        testData.add(
            VisitData(
                carryingElectronic = "No",
                visitArea = "Building B",
                visitDate = "2024-04-02",
                visitOrderStatus = "1",
                visitPurpose = "Interview",
                visitingPersonMobileNo = "0987654321",
                visitingPersonName = "Jane Doe",
                visitorEmployer = "XYZ Inc.",
                visitorIdNo = "6543210987654321",
                visitorMobileNo = "1234567890",
                visitorName = "Bob Johnson",
                followPersonVos = null
            )
        )

        return testData
    }

    private fun generateFakeVisitDataList(count: Int): List<VisitData> {
        val visitDataList = mutableListOf<VisitData>()
        repeat(count) {
            visitDataList.add(
                VisitData(
                    carryingElectronic = if (it % 2 == 0) "是" else "否",
                    followPersonVos = listOf(
                        FollowPersonVo(
                            followEmployer = "单位 $it",
                            followIdNo = "身份证${it}123456",
                            followMobileNo = "987654321${it}",
                            followName = "随访人员 $it"
                        ),
                        FollowPersonVo(
                            followEmployer = "公司 $it",
                            followIdNo = "身份证${it}654321",
                            followMobileNo = "123456789${it}",
                            followName = "人员 $it"
                        )
                    ),
                    visitArea = "地区 $it",
                    visitDate = "2024-04-0${it + 1}",
                    visitOrderStatus = if (it % 2 == 0) "1" else "0",
                    visitPurpose = "目的 $it",
                    visitingPersonMobileNo = "123456789${it}",
                    visitingPersonName = "拜访者 $it",
                    visitorEmployer = "单位 $it",
                    visitorIdNo = "身份证${it}789012",
                    visitorMobileNo = "567890123${it}",
                    visitorName = "访客 $it"
                )
            )
        }
        return visitDataList
    }

    private fun showDatePickerDialog(context: Context) {
        // 获取当前日期
        val calendar: Calendar = Calendar.getInstance()
        val year: Int = calendar.get(Calendar.YEAR)
        val month: Int = calendar.get(Calendar.MONTH)
        val dayOfMonth: Int = calendar.get(Calendar.DAY_OF_MONTH)

        // 创建日期选择Dialog，并设置默认选中的日期为当前日期
        val datePickerDialog = DatePickerDialog(
            context, R.style.CustomDatePickerDialogTheme,
            { _, year, month, dayOfMonth -> // 处理日期选择结果
                mBinding.tvDate.text = formatDate(year, month, dayOfMonth)
            }, year, month, dayOfMonth
        )
        // 设置日期选择Dialog的样式为日历样式
        datePickerDialog.datePicker.calendarViewShown = true
        datePickerDialog.datePicker.spinnersShown = false // 隐藏下拉式的年份和月份选择器
        // 显示日期选择Dialog
        datePickerDialog.show()
    }

    private fun initTest() {
        if (IS_TEST_MODE) {

        }
    }
}